﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Security;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace MyHttp
{
    /// <summary>
    /// 接口请求类型枚举类
    /// </summary>
    public enum MyHttpType
    {
        /// <summary>
        /// get请求，系统默认超时时间
        /// </summary>
        [Description("get请求，系统默认超时时间")]
        Get,

        /// <summary>
        /// post请求，系统默认超时时间
        /// </summary>
        [Description("post请求，系统默认超时时间")]
        Post,

        /// <summary>
        /// put请求，系统默认超时时间
        /// </summary>
        [Description("put请求，系统默认超时时间")]
        Put,

        /// <summary>
        /// patch请求，系统默认超时时间
        /// </summary>
        [Description("patch请求，系统默认超时时间")]
        Patch,

        /// <summary>
        /// delete请求，系统默认超时时间
        /// </summary>
        [Description("delete请求，系统默认超时时间")]
        Delete,

        /// <summary>
        /// post请求，上传文件
        /// </summary>
        [Description("post请求，上传文件")]
        UploadFile,
    }

    /// <summary>
    /// 
    /// </summary>
    public class MyHttpHelper
    {
        /// <summary>
        /// 验证服务器证书回调自动验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="certificate"></param>
        /// <param name="chain"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {   // 总是接受  
            return true;
        }

        /// <summary>
        /// 请求接口返回数据
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="param">参数</param>
        /// <param name="type">请求类型 get、put……</param>
        /// <returns>返回结果json字符串形式</returns>
        public string Request(string url, string param, MyHttpType type)
        {
            Task<string>? httpResult = null;
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证

                #region 调用接口
                switch (type)
                {
                    case MyHttpType.Post:
                        httpResult = MyHttpClientHelper.HttpPost(url, param);
                        break;
                    case MyHttpType.Put:
                        httpResult = MyHttpClientHelper.HttpPut(url, param);
                        break;
                    case MyHttpType.Patch:
                        httpResult = MyHttpClientHelper.HttpPatch(url, param);
                        break;
                    case MyHttpType.Get:
                        httpResult = MyHttpClientHelper.HttpGet(url + param);
                        break;
                    case MyHttpType.Delete:
                        httpResult = MyHttpClientHelper.HttpDelete(url + param);
                        break;
                    case MyHttpType.UploadFile:
                        break;
                }
                #endregion
            }
            catch
            {
                throw;
            }
            if (httpResult == null)
                return string.Empty;
            return httpResult.Result;
        }

    }
}
